package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.BackPay;
import com.ldj.hibernate.BackPayDAO;
import com.ldj.hibernate.CaseInfo;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.Payoff;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.BackPayForm;
import com.ldj.struts.form.PayoffForm;

public class BackPayAction extends DispatchAction {

	/**
	 * 监察员案件基本信息管理
	 */
	
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		String caseInfoMajorInspectorId = SystemSession.getSupUserId(request);
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String empInfoName = request.getParameter("empInfoName");
		List list = dao.findAllPage(caseInfoMajorInspectorId, syear, eyear, empInfoName);
		request.setAttribute("list", list);
		request.setAttribute("endYear", y);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		return mapping.findForward("list");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		BackPay info = dao.findById(request.getParameter("bid"));
		if(!info.getBackPayState().equals("0")){
			List poList = dao.findPayoffByBackPayId(info.getBackPayId());
			request.setAttribute("poList", poList);
		}
		request.setAttribute("bInfo", info);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year);
		request.setAttribute("endYear", y);
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		dao.getSession().flush();
		dao.getSession().clear();
		BackPay info = dao.findById(request.getParameter("bid"));
		request.setAttribute("bInfo", info);
		request.setAttribute("endYear", y);
		request.setAttribute("nowPage",request.getParameter("page"));
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayForm bForm = (BackPayForm)form;
		BackPayDAO dao = new BackPayDAO(request, response);
		BackPay info = dao.findById(request.getParameter("bid"));
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(bForm.getBackPayUnitId());
		info.setEmployerInfo(eInfo);
		info.setBackPayMonth(bForm.getBackPayMonth());
		info.setBackPayPensonNum(bForm.getBackPayPensonNum());
		info.setBackPayMoney(bForm.getBackPayMoney());
		info.setBackPayRemark(bForm.getBackPayRemark());
		dao.merge(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "BackPay.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		dao.getSession().flush();
		dao.getSession().clear();
		return new ActionForward("/BackPay.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayForm bForm = (BackPayForm)form;
		BackPay info = new BackPay();
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(bForm.getBackPayUnitId());
		info.setEmployerInfo(eInfo);
		info.setBackPayMonth(bForm.getBackPayMonth());
		info.setBackPayPensonNum(bForm.getBackPayPensonNum());
		info.setBackPayMoney(bForm.getBackPayMoney());
		info.setBackPayRemark(bForm.getBackPayRemark());
		info.setBackPayIsCase("0");
		info.setBackPayState("0");
		info.setBackPayCreateTime(new Date());
		info.setBackPayCreator(SystemSession.getSupUserId(request));
		BackPayDAO dao = new BackPayDAO(request, response);
		dao.save(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "BackPay.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		dao.getSession().flush();
		dao.getSession().clear();
		return new ActionForward("/BackPay.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String userId = SystemSession.getSupUserId(request);
		BackPayDAO dao = new BackPayDAO(request, response);
		List list = dao.findFromIdArray(userId, request.getParameterValues("bid"));
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				BackPay info = (BackPay)list.get(i);
				dao.delete(info);
			}
		}
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "BackPay.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		dao.getSession().flush();
		dao.getSession().clear();
		return new ActionForward("/BackPay.do?method=list&pages="+request.getParameter("page"), true);
	}
	
	public ActionForward sbt(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		BackPay info = dao.findById(request.getParameter("bid"));
		info.setBackPayState("1");
		dao.merge(info);
		dao.getSession().flush();
		dao.getSession().clear();
		return new ActionForward("/BackPay.do?method=detail&bid=" + info.getBackPayId(), true);
	}
	
	public ActionForward over(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		BackPay info = dao.findById(request.getParameter("bid"));
		info.setBackPayState("2");
		dao.merge(info);
		dao.getSession().flush();
		dao.getSession().clear();
		return new ActionForward("/BackPay.do?method=detail&bid=" + info.getBackPayId(), true);
	}
	
	public ActionForward toCase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		BackPay info = dao.findById(request.getParameter("bid"));
		
		CaseInfo cInfo = new CaseInfo();
		cInfo.setEmployerInfo(info.getEmployerInfo());
		cInfo.setCaseInfoBasiccondiBeginres(info.getBackPayRemark());
		cInfo.setCaseInfoSource("005005");
		request.setAttribute("cInfo", cInfo);
		info.setBackPayIsCase("1");
		dao.merge(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("11");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "BackPay.case").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		dao.getSession().flush();
		dao.getSession().clear();
		return mapping.findForward("toCase");
	}
	
	/**
	 * 补发工资记录
	 */
	public ActionForward toPayoffAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward addPayoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PayoffForm pForm = (PayoffForm)form;
		BackPayDAO dao = new BackPayDAO(request, response);
		Payoff info = new Payoff();
		info.setBackPayId(pForm.getBackPayId());
		info.setPayoffPensonNum(pForm.getPayoffPensonNum());
		info.setPayoffMoney(pForm.getPayoffMoney());
		info.setPayoffDate(pForm.getDatePayoffDate());
		dao.save(info);
		dao.getSession().flush();
		dao.getSession().clear();
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "Payoff.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		return new ActionForward("/Payoff.do?method=detailPayoff&pid=" + info.getPayoffId(), true);
	}
	
	public ActionForward toPayoffUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		Payoff info = dao.findPayoffById(request.getParameter("pid"));
		request.setAttribute("pInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward updatePayoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PayoffForm pForm = (PayoffForm)form;
		BackPayDAO dao = new BackPayDAO(request, response);
		Payoff info = dao.findPayoffById(request.getParameter("pid"));
		info.setPayoffPensonNum(pForm.getPayoffPensonNum());
		info.setPayoffMoney(pForm.getPayoffMoney());
		info.setPayoffDate(pForm.getDatePayoffDate());
		dao.merge(info);
		dao.getSession().flush();
		dao.getSession().clear();
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "Payoff.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		return new ActionForward("/Payoff.do?method=detailPayoff&pid=" + info.getPayoffId(), true);
	}
	
	public ActionForward detailPayoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		Payoff info = dao.findPayoffById(request.getParameter("pid"));
		request.setAttribute("pInfo", info);
		return mapping.findForward("detail");
	}
	
	public ActionForward deletePayoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BackPayDAO dao = new BackPayDAO(request, response);
		Payoff info = dao.findPayoffById(request.getParameter("pid"));
		String pid = info.getBackPayId();
		dao.delete(info);
		dao.getSession().flush();
		dao.getSession().clear();
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "Payoff.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()));
		logDao.save(s);
		return new ActionForward("/BackPay.do?method=detail&bid=" + pid, true);
	}

}